package test3;

import java.io.IOException;
import java.net.*;

public class ClinetDemo {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(9099);

        String message = "服务端你好啊！";
        byte[] bytes = message.getBytes();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 8888;
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
        ds.send(dp);

        DatagramPacket dp2 = new DatagramPacket(new byte[1024], 1024);
        System.out.println("未接收");
        ds.receive(dp2);
        System.out.println("接收消息");
        byte[] data = dp2.getData();
        int length = dp2.getLength();
        System.out.println(new String(data, 0, length));

        ds.close();
    }
}
